package cc.rengu.redp.bizimpl.csmc.service.impl;

import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.csmc.dao.TCMchntAcctSumMapper;
import cc.rengu.redp.bizimpl.csmc.domain.TCMchntAcctDetail;
import cc.rengu.redp.bizimpl.csmc.domain.TCMchntAcctSum;
import cc.rengu.redp.bizimpl.csmc.service.ITCTblMchntAcctRsltService;
import cc.rengu.redp.bizimpl.upmp.domain.BTxnLimitCfg;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpBMchntBaseInfo;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpSysParam;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.render.dict.cache.DictCacheService;
import cc.rengu.redp.common.render.dict.sources.DictData;
import cc.rengu.redp.common.utils.SortUtil;
import cc.rengu.redp.system.domain.Dict;
import cc.rengu.redp.system.utils.DictUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author System
 */
@Service
public class TCMchntAcctSumServiceImpl extends ServiceImpl<TCMchntAcctSumMapper, TCMchntAcctSum> implements ITCTblMchntAcctRsltService {

    @Autowired
    IUpmpSysParamService upmpSysParamService;

    /**
     * 分頁
     */
    @Override
    public IPage<TCMchntAcctSum> findPage(QueryRequest request, TCMchntAcctSum tcMchntAcctSum) {
        try {
            String instId = "";
            //获取当前用户的法人id
            UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
            boolean flag = user.isAdmin();
            if (flag) {
                instId = tcMchntAcctSum.getInstId();
            }
            if (!(flag) || StringUtils.isBlank(instId)) {
                instId = user.getInstId();
            }
            if (StringUtils.isBlank(instId)) {
                throw new RedpException("多法人机构号为空");
            }
            //条件查询
            LambdaQueryWrapper<TCMchntAcctSum> queryWrapper = new LambdaQueryWrapper<>();
            //机构标识
            if (!StringUtils.isBlank(tcMchntAcctSum.getInstId())) {
                queryWrapper.eq(TCMchntAcctSum::getInstId, instId);
            }

            //商户入账核心记账日期
            if (!StringUtils.isBlank(tcMchntAcctSum.getStartDate())) {
                queryWrapper.ge(TCMchntAcctSum::getAcctDate,tcMchntAcctSum.getStartDate())
                        .le(TCMchntAcctSum::getAcctDate, tcMchntAcctSum.getEndDate());
            }
//             商户净额清算日期
            if (!StringUtils.isBlank(tcMchntAcctSum.getNetSettleDate())) {
                queryWrapper.ge(TCMchntAcctSum::getNetSettleDate, tcMchntAcctSum.getNetSettleDate());
            }
            if(!StringUtils.isBlank(tcMchntAcctSum.getNetSettleDate())) {
                queryWrapper.le(TCMchntAcctSum::getNetSettleDate, tcMchntAcctSum.getEndDateQs());
            }
            //
//            商户结算账户
            if (!StringUtils.isBlank(tcMchntAcctSum.getMchntSettleAccount())) {
                    //like
                    queryWrapper.eq(TCMchntAcctSum::getMchntSettleAccount, tcMchntAcctSum.getMchntSettleAccount());
            }

//            商户入账核心记账状态
            if (!StringUtils.isBlank(tcMchntAcctSum.getAcctStatus())) {
                if("01".equals(tcMchntAcctSum.getAcctStatus())){
                    queryWrapper.eq(TCMchntAcctSum::getAcctStatus, tcMchntAcctSum.getAcctStatus());
                }else{
                    queryWrapper.ne(TCMchntAcctSum::getAcctStatus,"01");
                }
            }
            // 内部商户号
            if (!StringUtils.isBlank(tcMchntAcctSum.getMchntNo())) {
                queryWrapper.eq(TCMchntAcctSum::getMchntNo, tcMchntAcctSum.getMchntNo());
            }




            //分组  商户入账核心记账日期
            queryWrapper.orderByDesc(TCMchntAcctSum::getAcctDate);

            //分页
            Page<TCMchntAcctSum> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            Page<TCMchntAcctSum> cfgPage = this.page(page, queryWrapper);


            //数据字典 （待实现）
            //Map<String, Map<String, String>> teerrtxninfo = dictData.getAllDictData();
            return cfgPage;
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }

    @Override
    public IPage<TCMchntAcctSum> gridExportAll(QueryRequest request, TCMchntAcctSum tcMchntAcctSum) {
        try {
            IPage<TCMchntAcctSum> gridExportAll =this.findPage(request, tcMchntAcctSum);
            return gridExportAll;
        } catch (Exception e) {
            log.error("导出商户入账结果查询失败", e);
            return null;
        }
    }
}
